﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Phoenix.Service.Contracts;
using Phoenix.Service;
using Phoenix.Service.Model;
namespace Phoenix.AdminSite.Controllers
{
    public class RightsTypeController : ControllerBase
    {
        IRightsTypeService IRightsType = new RightsTypeService();
        IIDCreaterService IIDCreater = new IDCreaterService();
        public ActionResult Index()
        {
            ViewData["RightsTypeList"] = IRightsType.GetAll();

            return View();
        }

        public ActionResult Edit()
        {
			int typeID = 0;
			if (!string.IsNullOrEmpty(Request.QueryString["typeid"]))
			{
				typeID = int.Parse(Request.QueryString["typeid"]);

				ViewData["RightsTypeModel"] = IRightsType.GetByTypeID(typeID);
			}

			return View();
        }

		public ActionResult Save(FormCollection collection)
		{
			RightsTypeModel model = new RightsTypeModel();
			model.TypeName = collection["txtTypeName"];
			model.SystemFlag = 1;
			model.ParentTypeID = 0;
			model.CreateUser = 1;
			model.CreateTime = DateTime.Now;
			model.UpdateTime = DateTime.MinValue;
			model.UpdateUser = 0;


			if (!string.IsNullOrEmpty(collection["hdTypeID"]))
			{
				model.TypeID = int.Parse(collection["hdTypeID"]);
				model.UpdateTime = DateTime.Now;
				IRightsType.UpdateByTypeID(model);
			}
			else
			{
                model.TypeID = IIDCreater.GetNewID("RightTypeID", "权限列别ID", 100);
				IRightsType.AddRightsType(model);

			}

			return ShowMsg("成功", Common.ActionStatus.OK, "");
		}

        public ActionResult Delete(int typeID)
        {
            ///TODO:删除前检查是否已经被使用
            IRightsType.DeleteRightAndType(typeID);
            return ShowMsg("删除成功", Common.ActionStatus.OK, "");
        }

    }
}
